package com.huimin.merchant.controller;

import com.huimin.merchant.common.util.SecurityUtil;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.service.PlatformChannelServiceApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author amx
 * @date 2020/12/6 22:30
 */
@RestController
@Api(tags = "平台服务类型接口")
public class PlatformChannelController {

	@Reference
	private PlatformChannelServiceApi platformChannelService;

	@ApiOperation("获取平台服务类型")
	@GetMapping(value = "/my/platform-channels")
	public List<PlatformChannelDTO> getAllPlatformChannels() {
		return platformChannelService.getAllPlatformChannel();
	}

	@ApiOperation("绑定服务类型")
	@PostMapping(value = "/my/apps/{appId}/platform-channels")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "应用id", name = "appId",
					dataType = "string", paramType = "path"),
			@ApiImplicitParam(value = "服务类型code", name = "platformChannelCodes",
					dataType = "string", paramType = "query")
	})
	public void bindPlatformForApp(@PathVariable("appId") String appId,
	                               @RequestParam("platformChannelCodes") String platformChannelCodes) {
		platformChannelService.bindPlatformForApp(appId, platformChannelCodes);
	}

	@ApiOperation("查询应用是否绑定了某个服务类型")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "appId", value = "应用appId",
					required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "platformChannel", value = "服务类型",
					required = true, dataType = "String", paramType = "query")
	})
	@GetMapping("/my/merchants/apps/platformchannels")
	public int queryAppBindPlatformChannel(@RequestParam("appId") String appId,
	                                       @RequestParam("platformChannel") String platformChannelCodes) {
		return platformChannelService.existBindPlatformForApp(appId, platformChannelCodes);
	}

	@ApiOperation("根据平台服务类型获取支付渠道列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "platformChannelCode", value = "服务类型编码",
					required = true, dataType = "String", paramType = "path")
	})
	@GetMapping(value = "/my/pay-channels/platform-channel/{platformChannelCode}")
	public List<PayChannelDTO> findPayChannelsByPlatformChannelCode(@PathVariable("platformChannelCode") String platformChannelCode) {
		return platformChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
	}

	@ApiOperation("商户配置支付渠道参数")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "payChannelParam", value = "商户配置支付渠道参数",
					required = true, dataType = "PayChannelParamDTO", paramType = "body")
	})
	@RequestMapping(value = "/my/pay-channel-params", method = {RequestMethod.POST, RequestMethod.PUT})
	public PayChannelParamDTO savePayChannelParam(@RequestBody PayChannelParamDTO payChannelParam) {
		Long merchantId = SecurityUtil.getMerchantId();
		payChannelParam.setMerchantId(merchantId);
		return platformChannelService.savePayChannelParam(payChannelParam);
	}

	@ApiOperation("获取指定应用指定服务类型下所包含的原始支付渠道参数列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "appId", value = "应用id",
					required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "platformChannel", value = "服务类型",
					required = true, dataType = "String", paramType = "path")})
	@GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
	public List<PayChannelParamDTO> queryPayChannelParam(@PathVariable String appId,
	                                                     @PathVariable String platformChannel) {

		return platformChannelService.queryPayChannelParamByAppAndPlatform(appId, platformChannel);
	}

	@ApiOperation("获取指定应用指定服务类型下所包含的原始支付渠道参数")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "appId", value = "应用id",
					required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "payChannel", value = "第三方渠道code",
					required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "platformChannel", value = "服务类型",
					required = true, dataType = "String", paramType = "path")})
	@GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels" +
			"/{payChannel}")
	public PayChannelParamDTO queryPayChannelParam(@PathVariable String appId,
	                                               @PathVariable String platformChannel,
	                                               @PathVariable String payChannel
	) {

		return platformChannelService.queryParamByAppPlatformAndPayChannel(appId,
		                                                                   platformChannel,
		                                                                   payChannel);
	}


}
